﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TorchView.GameContent.Data;

namespace TorchView.GameContent.Affixes
{
    public class AffixUnitTypes
    {
        public const string BLOCK_NAME = "UNITTYPES";
        private static readonly string[] MELEE = 
        {
            "AXE",
            "MACE",
            "POLEARM",
            "STAFF",
            "SWORD"
        };
        private static readonly string[] ARMOR = 
        {
            "BELT",
            "BOOTS",
            "CHEST ARMOR",
            "GLOVES",
            "HELMET",
            "SHIELD",
            "SHOULDER ARMOR"
        };
        private static readonly string[] TRINKET =
        {
            "NECKLACE",
            "RING"
        };
        private static readonly string[] RANGED = 
        {
            "BOW",
            "CROSSBOW",
            "PISTOL",
            "RIFLE",
            "WAND"
        };

        public AffixUnitTypes()
        {
            UnitTypes = new List<string>();
        }

        public List<string> UnitTypes { get; private set; }

        public static AffixUnitTypes LoadFromDataBlock(DataBlock block)
        {
            if (block == null)
                throw new ArgumentNullException("block");
            if (block.Name != BLOCK_NAME)
                throw new ArgumentException("Incorrect block type, only " + BLOCK_NAME + " is supported.");

            AffixUnitTypes types = new AffixUnitTypes();

            foreach(DataProperty property in block.Properties)
            {
                if (property.Name == "UNITTYPES")
                    types.UnitTypes.Add(property.Value.ToString());
            }

            return types;
        }

        public override string ToString()
        {
            return string.Join(", ", UnitTypes.ToArray());
        }

        public bool HasUnitType(string inType)
        {
            foreach(string type in UnitTypes)
            {
                switch (type)
                {
                    case "MELEE":
                        if(ArrayCompare(MELEE,inType))
                            return true;
                        break;
                    case "RANGED WEAPON":
                        if(ArrayCompare(RANGED,inType))
                            return true;
                        break;
                    case "WEAPON":
                        if(ArrayCompare(MELEE,inType))
                            return true;
                        if(ArrayCompare(RANGED,inType))
                            return true;
                        break;
                    case "ARMOR":
                        if(ArrayCompare(ARMOR,inType))
                            return true;
                        break;
                    case "TRINKET":
                        if(ArrayCompare(TRINKET,inType))
                            return true;
                        break;
                    default:
                        if(inType.Contains(type))
                            return true;
                        break;
                }
            }
            return false;
        }

        private bool ArrayCompare(string[] array, string inType)
        {
            foreach (string type in array)
            {
                if (inType.Contains(type))
                    return true;
            }
            return false;
        }
    }
}
